//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.doc.controller;

import com.farm.core.page.ViewMode;
import com.farm.doc.domain.FarmDocfile;
import com.farm.doc.server.FarmFileManagerInter;
import com.farm.doc.server.FarmFileManagerInter.FILE_TYPE;
import com.farm.parameter.FarmParameterService;
import com.farm.web.WebUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@RequestMapping({"/actionImg"})
@Controller
public class ActionImgQuery extends WebUtils {
    private static final Logger log = Logger.getLogger(ActionImgQuery.class);
    @Resource
    private FarmFileManagerInter farmFileManagerImpl;

    public ActionImgQuery() {
    }

    @RequestMapping({"/PubFPupload.do"})
    @ResponseBody
    public Map<String, Object> upload(@RequestParam(value = "imgFile",required = false) MultipartFile file, String limittypes, HttpServletRequest request, ModelMap model, HttpSession session) {
        String url = null;
        String id = null;
        String fileName = "";

        byte error;
        String message;
        try {
            String fileid = null;
            String maxLength = FarmParameterService.getInstance().getParameter("config.doc.upload.length.max");
            if (file.getSize() > (long)Integer.valueOf(maxLength).intValue()) {
                throw new Exception("文件不能超过" + Integer.valueOf(maxLength).intValue() / 1024 + "kb");
            }

            CommonsMultipartFile cmFile = (CommonsMultipartFile)file;
            DiskFileItem item = (DiskFileItem)cmFile.getFileItem();
            if (!item.getStoreLocation().exists()) {
                item.write(item.getStoreLocation());
            }

            String typesstr = FarmParameterService.getInstance().getParameter("config.doc.upload.types");
            if (limittypes != null && !limittypes.isEmpty()) {
                typesstr = limittypes;
            }

            fileName = URLEncoder.encode(item.getName(), "utf-8");
            List<String> types = this.parseIds(typesstr.toUpperCase().replaceAll("，", ","));
            if (!types.contains(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1, file.getOriginalFilename().length()).toUpperCase())) {
                throw new Exception("文件类型错误，允许的类型为：" + typesstr.toUpperCase().replaceAll("，", ","));
            }

            fileid = this.farmFileManagerImpl.saveFile(item.getStoreLocation(), FILE_TYPE.RESOURCE_FILE, file.getOriginalFilename(), this.getCurrentUser(session));
            error = 0;
            url = this.farmFileManagerImpl.getFileURL(fileid);
            message = "";
            id = fileid;
        } catch (Exception var17) {
            var17.printStackTrace();
            error = 1;
            message = var17.getMessage();
        }

        return ViewMode.getInstance().putAttr("error", Integer.valueOf(error)).putAttr("url", url).putAttr("message", message).putAttr("id", id).putAttr("fileName", fileName).returnObjMode();
    }

    @RequestMapping({"/PubFPuploadImg.do"})
    @ResponseBody
    public Map<String, Object> PubFPuploadImg(@RequestParam(value = "imgFile",required = false) MultipartFile file, HttpServletRequest request, ModelMap model, HttpSession session) {
        log.debug("go to ImgUpload");
        String url = null;
        String id = null;
        String fileName = "";

        byte error;
        String message;
        try {
            String fileid = null;
            String maxLength = FarmParameterService.getInstance().getParameter("config.doc.upload.length.max");
            if (file.getSize() > (long)Integer.valueOf(maxLength).intValue()) {
                throw new Exception("文件不能超过" + Integer.valueOf(maxLength).intValue() / 1024 + "kb");
            }

            CommonsMultipartFile cmFile = (CommonsMultipartFile)file;
            DiskFileItem item = (DiskFileItem)cmFile.getFileItem();
            if (!item.getStoreLocation().exists()) {
                item.write(item.getStoreLocation());
            }

            fileName = URLEncoder.encode(item.getName(), "utf-8");
            List<String> types = this.parseIds(FarmParameterService.getInstance().getParameter("config.doc.img.upload.types").toUpperCase().replaceAll("，", ","));
            if (!types.contains(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1, file.getOriginalFilename().length()).toUpperCase())) {
                throw new Exception("文件类型错误，允许的类型为：" + FarmParameterService.getInstance().getParameter("config.doc.img.upload.types").toUpperCase().replaceAll("，", ","));
            }

            fileid = this.farmFileManagerImpl.saveFile(item.getStoreLocation(), FILE_TYPE.RESOURCE_IMG, file.getOriginalFilename(), this.getCurrentUser(session));
            error = 0;
            url = this.farmFileManagerImpl.getFileURL(fileid);
            message = "";
            id = fileid;
        } catch (Exception var15) {
            error = 1;
            message = var15.getMessage();
        }

        return ViewMode.getInstance().putAttr("error", Integer.valueOf(error)).putAttr("url", url).putAttr("message", message).putAttr("id", id).putAttr("fileName", fileName).returnObjMode();
    }

    @RequestMapping({"/Publoadfile"})
    public void download(String id, HttpServletRequest request, HttpServletResponse response) {
        FarmDocfile file = null;

        try {
            file = this.farmFileManagerImpl.getFile(id);
        } catch (Exception var47) {
            file = null;
        }

        if (file == null || file.getFile() == null || !file.getFile().exists()) {
            file = new FarmDocfile();
            file.setFile(this.farmFileManagerImpl.getNoneImg());
            file.setName("none");
        }

        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");

        try {
            response.setHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("gbk"), "iso-8859-1"));
        } catch (UnsupportedEncodingException var46) {
            var46.printStackTrace();
        }

        InputStream is = null;
        ServletOutputStream os = null;

        try {
            is = new FileInputStream(file.getFile());
            os = response.getOutputStream();
            byte[] b = new byte[2048];

            int length;
            while((length = is.read(b)) > 0) {
                os.write(b, 0, length);
            }
        } catch (FileNotFoundException var50) {
            FileNotFoundException e = var50;
            InputStream is1 = null;
            ServletOutputStream os1 = null;

            try {
                String webPath = FarmParameterService.getInstance().getParameter("farm.constant.webroot.path");
                String filePath = "/WEB-FACE/img/style/nullImg.png".replaceAll("/", File.separator.equals("/") ? "/" : "\\\\");
                File nullFile = new File(webPath + filePath);
                is1 = new FileInputStream(nullFile);
                os1 = response.getOutputStream();
                byte[] b = new byte[2048];

                int length;
                while((length = is1.read(b)) > 0) {
                    os1.write(b, 0, length);
                }
            } catch (Exception var48) {
                log.error(e.getMessage());
            } finally {
                try {
                    is1.close();
                    os1.close();
                } catch (IOException var45) {
                    log.error(var50.getMessage());
                }

            }

            log.error(var50.getMessage());
        } catch (IOException var51) {
            if (var51.getClass().getName().indexOf("ClientAbortException") < 0) {
                log.error(var51.getMessage());
            }
        } finally {
            try {
                is.close();
                os.close();
            } catch (IOException var44) {
                ;
            }

        }

    }

    @RequestMapping({"/Publogo"})
    public void logo(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        InputStream is1 = null;
        ServletOutputStream os1 = null;

        try {
            String webPath = FarmParameterService.getInstance().getParameter("farm.constant.webroot.path");
            String filePath = "/view/web-simple/atext/png/icon".replaceAll("/", File.separator.equals("/") ? "/" : "\\\\");
            File nullFile = new File(webPath + filePath);
            is1 = new FileInputStream(nullFile);
            os1 = response.getOutputStream();
            byte[] b = new byte[2048];

            int length;
            while((length = is1.read(b)) > 0) {
                os1.write(b, 0, length);
            }
        } catch (Exception var19) {
            log.error(var19.getMessage());
        } finally {
            try {
                is1.close();
                os1.close();
            } catch (IOException var18) {
                log.error(var18.getMessage());
            }

        }

    }

    @RequestMapping({"/PubHomelogo"})
    public void maxlogo(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        InputStream is1 = null;
        ServletOutputStream os1 = null;

        try {
            String webPath = FarmParameterService.getInstance().getParameter("farm.constant.webroot.path");
            String filePath = "/view/web-simple/atext/png/maxicon".replaceAll("/", File.separator.equals("/") ? "/" : "\\\\");
            File nullFile = new File(webPath + filePath);
            is1 = new FileInputStream(nullFile);
            os1 = response.getOutputStream();
            byte[] b = new byte[2048];

            int length;
            while((length = is1.read(b)) > 0) {
                os1.write(b, 0, length);
            }
        } catch (Exception var19) {
            log.error(var19.getMessage());
        } finally {
            try {
                is1.close();
                os1.close();
            } catch (IOException var18) {
                log.error(var18.getMessage());
            }

        }

    }
}
